package com.gs.api.core;

import io.xjar.XConstants;
import io.xjar.XKit;
import io.xjar.boot.XBoot;
import io.xjar.key.XKey;
import org.springframework.util.DigestUtils;

import java.security.MessageDigest;


/**
 * @author guoyunfei on 2021/9/16 19:30
 * @className EncryptedPack
 * @description TODO
 * @Version 1.0
 */
public class EncryptedPack {

    public static void main(String[] args) {



        try {

            // Spring-Boot Jar包加密
            String password = "你真牛逼被你破解了、给你点个超级的大大大赞。guoyunfei970418";
            //MD5在加密
            String md5 = convertMD5(string2MD5(password));
            XKey xKey = XKit.key(md5);
            //参数1是我们打包的文件目录和名称，参数2是加密后的文件路径和名称，参数3是密码
            XBoot.encrypt("D:\\IDEA\\bz-boot-admin\\target\\bz-boot-admin.jar",
                    "D:\\workingSpace\\MrGuo\\boot-api-test.jar", xKey,XConstants.MODE_DANGER);
            System.out.println("Successfully generated encrypted jar");

            // Spring-Boot Jar包解密
            /*String password = "guoyunfei970418";
            XKey xKey = XKit.key(password);
            XBoot.decrypt("D:\\IDEA\\bz-boot-api\\boot-api-test.jar", "D:\\IDEA\\bz-boot-api\\boot-api-test2.jar", xKey);
            System.out.println("Successfully generated decrypt jar");
            */

            /*
            String s = "guoyunfei0418";
            System.out.println("原始：" + s);
            System.out.println("MD5后：" + string2MD5(s));
            System.out.println("加密的：" + convertMD5(s));
            System.out.println("解密的：" + convertMD5(convertMD5(s)));
            */
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /***
     * MD5加码 生成32位md5码
     */
    public static String string2MD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++) {
            byteArray[i] = (byte) charArray[i];
        }
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16){
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();

    }

    /**
     * 加密解密算法 执行一次加密，两次解密
     */
    public static String convertMD5(String inStr) {

        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++) {
            a[i] = (char) (a[i] ^ 't');
        }
        String s = new String(a);
        return s;

    }



}
